<template>
  <page-header-wrapper>
    <tabList :tabList="tabList">
      <div class="searchBox">
        <div class="select">
          <span>商户：</span>
          <a-input placeholder="请输入商户名" v-model="queryParam.order_no">
            <a-icon slot="suffix" type="search" />
          </a-input>
        </div>
        <div class="select">
          <span>商品名称：</span>
          <a-input placeholder="请输入商品名称" v-model="queryParam.order_no">
            <a-icon slot="suffix" type="search" />
          </a-input>
        </div>
        <a-button type="primary" @click="getList">查询</a-button>
      </div>
    </tabList>
    <a-card :bordered="false" class="card_style_re" style="margin-top: 20px">
      <a-table
        :rowKey="
          (r, i) => {
            return i
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
      >
        <div slot="name" slot-scope="text, record">
          <div class="name">
            <div class="pic">
              <img :src="record.shop_goods_info.cover_pic" alt="" />
            </div>
            <div class="info">
              <span>{{ record.shop_goods_info.name }}</span>
              <span>{{
                record.shop_goods_attr_info
                  ? record.shop_goods_attr_info.map((item) => item.attr_name).join()
                  : '默认'
              }}</span>
            </div>
          </div>
        </div>
        <div slot="bindGood" slot-scope="text, record">
          <div class="name">
            <div class="pic">
              <img :src="record.goods_info.pic_url" alt="" />
            </div>
            <div class="info">
              <span>{{ record.goods_info.name }}</span>
              <span>{{ record.goods_info.attr ? record.goods_info.attr.attr_name : '暂无分类' }}</span>
            </div>
          </div>
        </div>
        <div slot="action" slot-scope="scope">
          <a-button type="link" @click="cancleBind(scope)">解除绑定</a-button>
        </div>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { bindRecord, bindGood } from '@/api/purchase/business'
import tabList from '@/views/saas/components/tabList.vue'
import { cyyUploadImg } from '@/components/CyyUi'
const columns = [
  {
    title: '商品名称',
    scopedSlots: {
      customRender: 'name',
    },
    width: 300,
  },
  {
    title: '绑定商品',
    scopedSlots: {
      customRender: 'bindGood',
    },
    width: 300,
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action',
    },
  },
]

export default {
  components: {
    tabList,
    cyyUploadImg,
  },
  data() {
    return {
      tabList: [
        {
          key: 'purchaseGoodsList',
          name: '商品列表',
        },
        {
          key: 'purchaseGoodsBand',
          name: '绑定商品记录',
        },
      ],
      // 搜索参数
      queryParam: {
        page: 1,
        limit: 10,
      },
      columns,
      list: [],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 关闭弹窗的回调
    closeModal() {},
    // 分页请求
    tablePageChange(e) {
      this.queryParam.page = e
      this.getList()
    },
    // 获取数据
    async getList() {
      const hide = this.$message.loading('加载中...', 1000)
      const res = await bindRecord(this.queryParam)
      if (res.code == 0) {
        this.list = res.data.list
        this.totalCount = res.data.count
      }
      hide()
    },
    // 解除绑定
    cancleBind(scope) {
      console.log(scope);
      const that = this
      this.$confirm({
        title: '是否确认解除绑定？',
        okText: '确认',
        cancelText: '取消',
        onOk() {
          let attr_id = scope.shop_goods_attr_ids.join('-')
          let goods_id = scope.shop_goods_info.id
          let cloud_goods_id = scope.goods_info.id
          let cloud_attr_id = scope.attr_id
          let param = { goods_id, attr_id, status: 0, cloud_goods_id, cloud_attr_id }
          bindGood(param).then((res) => {
            if (res.code == 0) {
              that.showBind = false
              that.getList()
            }
          })
        },
      })
    },
  },
}
</script>
<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    margin-top: 11px;
    display: flex;
    align-items: center;
    margin-right: 15px;
  }
  .ant-btn {
    margin-top: 11px;
  }
}
.tip {
  line-height: normal;
  margin-bottom: 0;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.45);
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}
</style>
